package cn.dglydrpy.study.algorithm.od;

/**
 * @author Yyy
 * @date 2024/11/5 12:14
 * @description
 * 题目描述：
 * 特定大小的停车场用数组表示，1 表示有车（小车占一车位，卡车占两车位，货车占三车位），0 表示没车，
 * 统计停车场最少可以停多少辆车并返回具体数目。
 */
public class Od0001_ParkStation {

    public static final String MARK = "#";
    
    public static void main(String[] args) {

//        String s = "1,1,1,0,1,1,1,1,0";

        // 交互式验证
//        Scanner scanner = new Scanner(System.in);
//        while (scanner.hasNext()) {
//            String s = scanner.next();
//            System.out.println(answer1(s));
//            System.out.println(myAnswer(s));
//        }

        // 对数器验证
        int testTime = 500000;
        boolean succeed = true;
        
        for (int i = 0; i < testTime; i++) {
            String s = genS();
            int answer1 = answer1(s);
            int myAnswer = myAnswer(s);
            if(answer1 != myAnswer){
                succeed = false;
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");
    }

    private static String genS() {
        StringBuilder stringBuilder = new StringBuilder();
        int length = (int) (Math.random()*1000) + 1;
        while(length > 0){
            double random = Math.random()*2;
            int c = random > 1 ? 1:0;
            stringBuilder.append(c).append(",");
            length--;
        }
        String s = stringBuilder.toString();
        return s.substring(0,s.length()-1);
    }

    private static int answer1(String s){
        int carCount = 0;
        String carMarks = s.replace(",", "")
                .replace("111", MARK)
                .replace("11", MARK)
                .replace("1", MARK);
        for (int i = 0; i < carMarks.length(); i++) {
            if ('#' == carMarks.charAt(i)) {
                carCount++;
            }
        }
        return carCount;
    } 
    
    private static int myAnswer(String s){
        if(s == null || s.length() == 0){
            return 0;
        }
        String[] cars = s.split(",");
        
        int ans = 0;
        int count1 = 0;
        for(int i = 0; i < cars.length; i++){
            
            if("0".equals(cars[i])){
                count1 = 0;
                continue;
            }

            // 后面当前位置的值是1
            
            if(count1 == 0){
                // 出现的第一个1
                ans++;
            }else{
                if("1".equals(cars[i-1])){
                    if(count1 == 3){
                        ans++;
                        count1 = 0;
                    }
                }
            }
            count1++;
        }
        
        return ans;
    }
    
    
}
